package com.meiyou.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.meiyou.pojo.EasyUIDataGrid;
import com.meiyou.pojo.MeiyouEvaluate;
import com.meiyou.pojo.MeiyouLyask;
import com.meiyou.pojo.MeiyouPtjls;
import com.meiyou.pojo.MeiyouTour;
import com.meiyou.pojo.MeiyouXtinform;
import com.meiyou.pojo.TourDetails;
import com.meiyou.pojo.vo.TourVo;
import com.meiyou.service.AppSettingService;
import com.meiyou.service.EvaContentService;
import com.meiyou.service.PtjlsService;
import com.meiyou.service.TourService;
import com.meiyou.service.UserService;
import com.meiyou.service.XtinformService;
import com.meiyou.utils.Msg;
import com.meiyou.utils.PagesMap;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "美游app控制器", tags = { "所有api接口" })
public class TourController {
	@Resource
	private TourService tourServiceImpl;
	@Resource
	private AppSettingService appsetting;
	@Resource
	private PtjlsService ptjlsServiceImpl;
	@Resource
	private UserService userServiceImpl;
	@Resource
	private XtinformService xtinformServiceImpl;
	@Resource
	private EvaContentService evaContentServiceImpl;

	@RequestMapping(value = "insTour", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "发布结伴旅游")
	public Msg Fabu(String tFaddress,String tTaddress,String tSex,String tSdate,String tCount,
		int tMeijin,String tPingtai,HttpServletRequest request ,String pay){
		if(request.getSession().getAttribute("user")==null) {
			System.out.println("session值位空");
			Msg.fail();
		}else {
		String uCount = request.getSession().getAttribute("user")+"";
		System.out.println("session值"+uCount);
		double meijin = Double.parseDouble(userServiceImpl.selUserByUcount(uCount));
		System.out.println(meijin);
		String num=appsetting.selFaBuJin();
		int fabujin=Integer.parseInt(num);
		System.out.println(fabujin);
		if(meijin>=fabujin+tMeijin){
			String payword = userServiceImpl.selUserPayByUcount(uCount);
			System.out.println(payword);
			    Date date = new Date();
				MeiyouTour meiyouTour = new MeiyouTour();
				meiyouTour.settTime(date);
				meiyouTour.settState("已发布");
				meiyouTour.settUCount(uCount);
				meiyouTour.settFaddress(tFaddress);
				meiyouTour.settMeijin(tMeijin);
				meiyouTour.settPingtai(tPingtai);
				meiyouTour.settSex(tSex);
				meiyouTour.settSdate(tSdate);
				meiyouTour.settTaddress(tTaddress);
				meiyouTour.settCount(tCount);
				int index=tourServiceImpl.insTour(meiyouTour);
				if(index>0){
					MeiyouPtjls meiyouPtjls=new MeiyouPtjls();
					meiyouPtjls.setpMeijin(fabujin);
					meiyouPtjls.setpTime(date);
					meiyouPtjls.setpUCount(uCount);
					int ptjls = ptjlsServiceImpl.insPtjls(meiyouPtjls);
					int upd = 0;
					if(tPingtai.equals("平台担保")){
						int chenyijin = Integer.parseInt(appsetting.selChenYiJin());
						double uMeijin=meijin-(tMeijin+fabujin+chenyijin);
						upd = userServiceImpl.updUserMeijin(uMeijin,uCount);
					}else{
					double uMeijin=meijin-(tMeijin+fabujin);
					upd = userServiceImpl.updUserMeijin(uMeijin,uCount);
					}
					if(ptjls>0 && upd>0){
						return Msg.success();
					}
				}
			}else {
				return Msg.fail();
			}
		}
		return Msg.success();
	}

	@RequestMapping(value = "selTourVo", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "显示结伴旅游信息")
	public List<TourVo> selTourVo(HttpServletRequest request) {
		String tUcount = request.getSession().getAttribute("user")+"";
		return tourServiceImpl.selTourVo(tUcount);
	}
	
	@RequestMapping(value = "selTourVoTerm", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "条件查询结伴旅游信息")
	public List<TourVo> selTourVo(String sex,HttpServletRequest request) {
		String tUcount = request.getSession().getAttribute("user")+"";
		return tourServiceImpl.selTourVoTerm(tUcount,sex);
	}
	
	@RequestMapping(value = "selTourDetails", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "详细旅游信息")
	public List<TourDetails> selTourDetails(int tId) {
		return tourServiceImpl.selTourDetails(tId);
	}
	
	@RequestMapping(value = "selCount", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查询报名人数")
	public int selCount(int lYId) {
		return tourServiceImpl.selCount(lYId);
	}

	@RequestMapping(value = "selLyask", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查询报名人选")
	public List<TourVo> selLyask(int tId) {
		return tourServiceImpl.selLyask(tId);
	}
	
	@RequestMapping(value = "updLyState", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "确认旅游人选")
	public Msg updState(int id,String xCount,HttpServletRequest request) {
		String uCount=request.getSession().getAttribute("user")+"";
		int index=tourServiceImpl.updState(id);
		if(index>0){
			MeiyouXtinform meiyouXtinform = new MeiyouXtinform();
			Date date = new Date();
			meiyouXtinform.setxCount(xCount);
			meiyouXtinform.setxContent(uCount+"已经确认了您的约会报名，请按时赴约");
			meiyouXtinform.setxTime(date);
			meiyouXtinform.setxUcount(uCount);
			xtinformServiceImpl.insXtinform(meiyouXtinform);
			return Msg.success();
		}
		return Msg.fail();
	}
	
	@RequestMapping(value = "delLyask", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除报名人的信息")
	public int delLyask(int lId) {
		return tourServiceImpl.delLyask(lId);
	}
	
	@RequestMapping(value = "lyPingxing", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "旅游评星")
	public Msg lyPingxing(int tAssess,int tId,String eUBcount,HttpServletRequest request){
		String eUCount = request.getSession().getAttribute("user")+"";
		Date date = new Date();
		MeiyouEvaluate meiyouEvaluate = new MeiyouEvaluate();
		int assess = tourServiceImpl.selTourAssess(tId);
		if(assess==0){
			tourServiceImpl.updAssess(tAssess, tId);
			meiyouEvaluate.setContent(Integer.toString(tAssess));
			meiyouEvaluate.seteUBcount(eUBcount);
			meiyouEvaluate.setTime(date);
			meiyouEvaluate.seteUCount(eUCount);
			meiyouEvaluate.setlId(tId);
			evaContentServiceImpl.insEvaluate(meiyouEvaluate);
			return Msg.success();
		}else {
			tAssess=(assess+tAssess)/2;
			tourServiceImpl.updAssess(tAssess, tId);
			meiyouEvaluate.setContent(Integer.toString(tAssess));
			meiyouEvaluate.seteUBcount(eUBcount);
			meiyouEvaluate.setTime(date);
			meiyouEvaluate.seteUCount(eUCount);
			meiyouEvaluate.setlId(tId);
			evaContentServiceImpl.insEvaluate(meiyouEvaluate);
			return Msg.success();
		}
	}
	
	@RequestMapping(value = "/delTour", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除结伴旅游信息")
	public int delTour(int tId) {
		return tourServiceImpl.delMyTour(tId);
	}

	@RequestMapping(value = "/showTour", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "后台分页显示旅游信息")
	public Msg showTour(int pn,MeiyouTour meiyouTour) {
		List<MeiyouTour> list = tourServiceImpl.showTour(meiyouTour);
		Map<String,Object> map = PagesMap.getpagesTourMaps(list, pn);
		return Msg.success().add("pageInfo", map);
	}

	@RequestMapping(value = "selAllTour", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "后台模糊查询旅游信息")
	public EasyUIDataGrid showAll(String tUCount, int rows, int page) {
		return tourServiceImpl.selAllTour(tUCount, rows, page);
	}
	
	@RequestMapping(value = "/TourApply", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "旅游报名")
	public Msg YuehuiApply(String uCount,String pay,int tId){
		double meijin = Double.parseDouble(userServiceImpl.selUserByUcount(uCount));
		String payword = userServiceImpl.selUserPayByUcount(uCount);
		if(payword!=null&&!payword.equals("")&&payword.equals(pay)){
			int Apply=1;
			double uMeijin=meijin-Apply;
			int upd = userServiceImpl.updUserMeijin(uMeijin, uCount);
			Date date = new Date();
			MeiyouPtjls meiyouPtjls=new MeiyouPtjls();
			meiyouPtjls.setpMeijin(Apply);
			meiyouPtjls.setpTime(date);
			meiyouPtjls.setpUCount(uCount);
			int ptjls = ptjlsServiceImpl.insPtjls(meiyouPtjls);
			if(ptjls>0&&upd>0){
				MeiyouLyask meiyouLyask=new MeiyouLyask();
				meiyouLyask.setlYId(tId);
				meiyouLyask.setlState("待确定");
				meiyouLyask.setlTime(date);
				meiyouLyask.setlUCount(uCount);
				int index = tourServiceImpl.insLyask(meiyouLyask);
				if(index>0){
				    return Msg.success();
				}else {
					return Msg.fail();
				}
			}
		}
		else{
			return Msg.fail();
		}
		return Msg.fail();
	}
	
}


